package com.apache.info.dao;

import java.util.List;

import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.DaoImpl;
import com.apache.database.model.MethodParam;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;

public class PubMetadataDaoImpl extends DaoImpl {

	public String getSql(int type) {
		String sql = "";
		switch (type) {
		case 1:
			sql = "select count(*) from PubMetadata where 1=1 ";
			break;
		case 2:
			sql = " from PubMetadata where 1=1 ";
			break;
		default:
			sql = "select count(*) from PubMetadata where 1=1 ";
		}
		return sql;
	}

	public void initSystemCache() {
		MethodParam param = new MethodParam("ByProperty", "", " from PubSortInfo", PubSortInfo.class.getName());
		List<PubSortInfo> list = this.select(param);
		if (!Validator.isEmpty(list)) {
			int ct = list.size();
			for (int i = 0; i < ct; i++) {
				PubSortInfo info = list.get(i);
				param = new MethodParam("ByProperty", "", " from PubMetadata", PubMetadata.class.getName());
				param.setParams("sortId", info.getSortCode());
				List<PubMetadata> lists = this.select(param);
				if (!Validator.isEmpty(list)) {
					String cacheKey = info.getSortCode() + "_tableAttr";
					StringBuffer attrs = new StringBuffer();
					String primaryKey = "";
					for (PubMetadata pm : lists) {
						attrs.append("," + pm.getDataAttr());
						if ("1".equals(pm.getDataRestrict())) {
							primaryKey = pm.getDataShortName();
						}
					}
					String cacheContent = cacheKey + ";" + attrs.substring(1).toString() + ";" + primaryKey + ";"
							+ info.getInfoTableName();
					SystemTools.getInstance().getCache("").createCacheObject(cacheKey, cacheContent);
				}
				SystemTools.getInstance().getCache("").createCacheObject("pubSortInfo_" + info.getSortId(), info);
			}
		}
	}
}
